package com.hyfd.task;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.ExceptionUtils;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;

public class LeMianSwTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;
	
	Logger log = Logger.getLogger(getClass());
	
	public void queryLeMianSwOrder(){
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = 2;
		try{
			String id = "0000000050";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);
			String defaultParameter = channel.get("default_parameter")+"";//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);//将参数解析成map
			String appId = paramMap.get("appId");//编号
			String userName = paramMap.get("userName");//账户
			String password = paramMap.get("password");//密码
			String queryUrl = "http://sdk.le-mian.com/JsonStatus.ashx";//查询地址
			
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectAll(param);//查询所有乐免省网的处理中订单
			for(Map<String,Object> order : orderList){
				
			}
		}catch(Exception e){
			log.error("乐免省网查询出错"+ExceptionUtils.getExceptionMessage(e));
		}
	}
	
	public void queryStatus(String queryUrl , String appId , String userName ,String password){
		password = MD5.ToMD5(password);//密码MD5加密
		String paramStr = "UserId="+appId+"&UserName="+userName+"&Password="+password;
		String result = "";
		try {
			result = ToolHttp.get(false, queryUrl+"?"+paramStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject resultJson = JSONObject.parseObject(result);
		String resultStatus = resultJson.getString("status");
		String resultDes = resultJson.getString("description");
		log.equals("乐免查询返回状态"+resultStatus+":"+resultDes);
		if(resultStatus.equals("1")){
			JSONArray reports = resultJson.getJSONArray("reports");
			for(int i = 0 ,size = reports.size() ; i < size ; i ++){
				JSONObject report = reports.getJSONObject(i);
				String mobile = report.getString("mobile");//手机号
				String msgid = report.getString("msgid");//上家订单号
				String status = report.getString("status");//状态
				if(status.equals("0000")){//充值成功
					
				}else{//充值失败
					
				}
			}
		}
	}
	
	/**
	 * 查询结果反馈
	 * @author lks 2017年4月8日下午3:13:30
	 * @param mobile
	 * @param msgid
	 */
	public static boolean statusConfirm(String mobile,String msgid,String UserId,String UserName,String Password){
		boolean flag = false;
		String confirmUrl = "http://sdk.le-mian.com/StatusConfirm.ashx";//结果反馈接口
		String paramStr = "UserId="+UserId+"&UserName="+UserName+"&Password="+MD5.ToMD5(Password)+"&msgid="+msgid+"&mobile="+mobile;//参数
		String result = "";
		try {
			result = ToolHttp.get(false, confirmUrl+"?"+paramStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(result.equals("1")){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 修改订单状态
	 * @author lks 2017年4月8日下午5:03:07
	 * @return
	 */
	public boolean changeStatus(String msgid,String status){
		boolean flag = false;
		Map<String,Object> order = orderDao.selectByProviderOrderId(msgid);//根据上家订单号获取订单数据
		if(status.equals("0000")){//状态成功
			order.put("status", "3");
			order.put("resultCode", "查询结果成功，状态码为"+status);
		}else{//状态失败
			order.put("status", "4");
			order.put("resultCode", "查询结果失败，状态码为"+status);
		}
		//更新数据库数据
		int num = orderDao.updateByPrimaryKeySelective(order);
		if(num>0){
			log.error("乐免查询更新订单状态成功");
			
		}else{
			log.error("乐免查询更新订单状态出错");
		}
		return flag;
	}
	
	
	
}
